package YUtils;


import java.io.*;

public abstract class FileUtils {
    public static int FILE_NOT_FOUND=-1;
    public static int IO_EXCEPTION=-2;
    public static int writeFile(File file, String content){
        try{
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(content.getBytes());
        }catch (FileNotFoundException e){
            e.printStackTrace();
            return FILE_NOT_FOUND;
        }catch (IOException e){
            e.printStackTrace();
            return IO_EXCEPTION;
        }
        return 0;
    }
    public static String readFile(File file){
        StringBuilder sb = new StringBuilder();
        try{
            BufferedReader br=new BufferedReader(new FileReader(file));
            String tmp;
            tmp=br.readLine();
            while (tmp!=null){
                sb.append(tmp).append('\n');
                tmp=br.readLine();
            }
        } catch (IOException e){
            e.printStackTrace();
            return null;
        }
        return sb.toString();
    }
    public static boolean deleteDir(String dirPath)
    {
        File file = new File(dirPath);
        if (!file.isFile()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File value : files) {
                    deleteDir(value.getPath());
                }
            }
        }
        return file.delete();
    }
}
